package com.sdry.system.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdry.system.entity.RoleMenu;
import com.sdry.system.entity.SysRole;
import com.sdry.system.mapper.RoleMapper;
import com.sdry.untils.UserContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;


/**
 * @program: info_sdry
 * @description: 角色管理
 * @author: cb
 * @create: 2020-02-12 19:58
 **/
@Transactional(rollbackFor = Exception.class)
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper,SysRole> implements RoleService{

    @Resource
    private RoleMapper roleMapper;

    /**
     * 查询所有的角色
     * @param role 角色对象
     * @return
     */
    @Override
    public IPage<SysRole> queryRolePage(SysRole role) {
        Page<SysRole> page = new Page<>(role.getPage(), role.getLimit());
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag",0L);
        return roleMapper.selectPage(page,queryWrapper);
    }

    /**
     * 添加角色
     * @param sysRole 角色对象
     * @param menus 菜单
     * @return
     */
    @Override
    public Integer addRole(SysRole sysRole, String[] menus) {
        Integer affect = 0;
        try {
            //角色
            sysRole.setDelFlag("0");
            Long userId = UserContext.getSessionUserIdByServiceImpl();
            sysRole.setCreateBy(userId.toString());
            affect = roleMapper.insert(sysRole);
            //角色菜单表
            if (menus != null) {
                for (String menu : menus) {
                    RoleMenu roleMenuData = new RoleMenu();
                    roleMenuData.setRoleId(sysRole.getRoleId());
                    roleMenuData.setMenuId(Long.parseLong(menu));
                    System.out.println("id = "+sysRole.getRoleId()+"----"+Long.parseLong(menu));
                    roleMapper.addRoleMenu(sysRole.getRoleId(),menu);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return affect;
    }

    /**
     * 添加或者修改角色
     * @param sysRole
     * @param menus
     * @return
     */
    @Override
    public Integer addOrUpdateRole(SysRole sysRole, String[] menus) {
        Integer affect = null;
        try {
            //角色
            sysRole.setDelFlag("0");
            Long userId = UserContext.getSessionUserIdByServiceImpl();
            //修改人员
            sysRole.setUpdateBy(userId.toString());
            //修改时间
            Timestamp updateDate = new Timestamp(System.currentTimeMillis());
            sysRole.setUpdateDate(updateDate);
            affect = roleMapper.updateById(sysRole);
            //角色菜单表
            roleMapper.deleteRoleMenu(sysRole.getRoleId());
            if (menus != null) {
                for (String menu : menus) {
                    RoleMenu roleMenuData = new RoleMenu();
                    roleMenuData.setRoleId(sysRole.getRoleId());
                    roleMenuData.setMenuId(Long.parseLong(menu));
                    roleMapper.addRoleMenu(sysRole.getRoleId(),menu);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return affect;
    }

    /**
     * 删除角色
     * @param roleId 主键
     * @return
     */
    @Override
    public Integer deleteRole(Long roleId) {
        Long userId = UserContext.getSessionUserIdByServiceImpl();
        return roleMapper.deleteRole(roleId,userId);
    }

    /**
     * 查询角色菜单
     * @param roleId
     * @return
     */
    @Override
    public List<Long> queryRoleMenuList(Long roleId) {
        return roleMapper.queryRoleMenuList(roleId);
    }

    @Override
    public List<Long> queryAppRoleMenuList(Long roleId) {
        return roleMapper.queryAppRoleMenuList(roleId);
    }

    @Override
    public Integer UpdateRoleAppMenu(SysRole sysRole, String[] menus) {
        Integer effect = 0;
        //角色菜单表
        if (menus != null) {
            effect = roleMapper.deleteRoleAppMenu(sysRole.getRoleId());
            for (String menu : menus) {
                roleMapper.addRoleAppMenu(sysRole.getRoleId(),menu);
            }
        }
        return effect;
    }
}
